package com.adee.java;

import java.util.concurrent.atomic.AtomicInteger;

public class Test042_AtomicInteger {

    static int counter = 0;

    static int max = 10;

    static AtomicInteger lock = new AtomicInteger(0); // 0-开锁，1-加锁

    public static void main(String[] args) {
        AtomicInteger ai = new AtomicInteger(Integer.MAX_VALUE);
        System.out.println(ai.get()); // 2147483647
        System.out.println(ai.incrementAndGet()); // -2147483648
        System.out.println(ai.incrementAndGet()); // -2147483647

    }

    public int increment() {
        while (lock.compareAndSet(0, 1)) {
            counter++;
            lock.set(0);
            return counter;
        }
        return 0;
    }
}
